package de.kardass.jsmatep.example;

import java.util.List;

import de.kardass.jsmatep.in.ImportProcessor;
import de.kardass.jsmatep.in.ImportResult;
import de.kardass.jsmatep.in.RecordMappedEvent;
import de.kardass.jsmatep.in.RecordMapperListener;

public class ExampleSimpleImporter implements RecordMapperListener<ExampleSimpleImportValueObject> {
	
	private static final String CONFIG_FILE = "Example-Import-Config-Variable-Length.xml";
	private static final String IMPORT_FILE = "example-simple-import-file.txt";
	
	private int recordCount = 0;
	
	public void printImportFile() {
		final ImportProcessor<ExampleSimpleImportValueObject> importProcessor = 
			new ImportProcessor<ExampleSimpleImportValueObject>(CONFIG_FILE, this);
		final ImportResult result = importProcessor.importFile(IMPORT_FILE);
		
		System.out.println();
		System.out.println("# SUMMARY #");
		
		// get (raw) skipped records while parsing records (empty rows or marked with #)
		final List<String> skippedRecords = result.getSkippedRecords();
		// get (raw) error records while parsing records (couldn't be parsed)
		final List<String> errorRecords = result.getErrorRecords();
		
		System.out.println("Count mapped value objects: " + result.getCountMappedValueObjects());
		System.out.println("Count skipped records:      " + skippedRecords.size());
		System.out.println("Count error records:        " + errorRecords.size());
	}
	
	/* (non-Javadoc)
	 * @see de.kardass.jsmatep.in.RecordMapperListener#afterRecordMapped(de.kardass.jsmatep.in.RecordMappedEvent)
	 */
	public void afterRecordMapped(RecordMappedEvent<ExampleSimpleImportValueObject> e) {
		++recordCount;
		System.out.println("Called for record " + recordCount + ": " + e.getMappedValueObject());
	}

	/**
	 * @param args
	 */
	public static void main(String... args) {
		new ExampleSimpleImporter().printImportFile();
	}

}
